package com.wjr.braveheart.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Created by WeiJinRan on 2018/1/24.
 * Email 994425089@qq.com.
 * Version 1.0
 * Describe: Activity基类的封装
 * PS: Not easy to write code, please indicate.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    protected Context mContext;
    /**
     * 上次点击时间
     */
    private long lastClick = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置Activity默认背景为透明，防止过度绘制（Overdraw）
        // 要在setContentView()方法之前执行
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setContentView(bindLayoutId());
        mContext = this;
        initEventAndData(savedInstanceState, getIntent().getExtras());
        loadData();
    }

    @Override
    protected void onDestroy() {
        unbindView();
        super.onDestroy();
    }


    @Override
    public void onClick(final View view) {
        if (!isFastClick()) onWidgetClick(view);
    }


    /**
     * 绑定布局
     *
     * @return layoutId
     */
    protected abstract int bindLayoutId();

    /**
     * 初始化视图以及数据操作
     *
     * @param savedInstanceState 保存实例状态
     * @param extras             Intent的传值
     */
    protected abstract void initEventAndData(Bundle savedInstanceState, Bundle extras);

    /**
     * 加载数据
     */
    protected abstract void loadData();

    /**
     * 防止多次点击
     *
     * @param view view
     */
    protected abstract void onWidgetClick(View view);

    /**
     * 解绑操作
     */
    protected abstract void unbindView();

    /**
     * 跳转 Activity
     *
     * @param clazz 目标Activity
     */
    public final void startActivity(Class<? extends Activity> clazz) {
        startActivity(clazz, null);
    }

    /**
     * 跳转 Activity
     *
     * @param clazz   起始Activity
     * @param options 目标Activity
     */
    public final void startActivity(Class<?> clazz, @Nullable Bundle options) {
        Intent intent = new Intent(this, clazz);
        if (options != null) {
            intent.putExtras(options);
        }
        startActivity(intent);
    }

    /**
     * 判断是否快速点击
     *
     * @return {@code true}: 是<br>{@code false}: 否
     */
    private boolean isFastClick() {
        long now = System.currentTimeMillis();
        if (now - lastClick >= 200) {
            lastClick = now;
            return false;
        }
        return true;
    }

    /**
     * 通过viewId获取控件
     *
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int viewId) {
        return (T) findViewById(viewId);
    }

    /**
     * 通过viewId获取控件
     *
     * @param parentView
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(View parentView, int viewId) {
        if (parentView == null) {
            throw new NullPointerException("parentView is null.");
        } else {
            return (T) parentView.findViewById(viewId);
        }
    }


    public Context getContext() {
        return mContext;
    }
}
